package com.example.yichuangyun.mvvm.ui.activity.view;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.model.call.CallRecordTransModel;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class CallRecordViewModel extends BaseViewModel {

    public MutableLiveData<String> urlSuccess = new MutableLiveData<>();
    public MutableLiveData<String> urlError = new MutableLiveData<>();
    public MutableLiveData<CallRecordTransModel> transTextSuccess = new MutableLiveData<>();

    /**
     * 获取录音url
     */
    public void getPcRecordUrl(String fileName) {
        Map<String, Object> map = new HashMap<>();
        map.put("fileName", fileName);
        map.put("isFollow", "0");
        addSubscribe(mRetrofitHelper.getPcRecordUrl(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        urlSuccess.setValue(baseModel.getData());
                    }
                } else {
                    urlError.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                urlError.setValue(message);
            }
        }));
    }

    /**
     * 录音文件转文字
     * type -1 第一次拉取   0 沟通内容   1 智能需求分析   2 沟通内容重新生成（已经废弃 不重复生成）
     */
    public void getTranscription(String recordId, String url, int type) {
        Map<String, Object> map = new HashMap<>();
        map.put("recordId", recordId);
        map.put("type", type);
        map.put("url", url);

        addSubscribe(mRetrofitHelper.getTranscription(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<CallRecordTransModel>() {
            @Override
            public void onSuccess(BaseModel<CallRecordTransModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        transTextSuccess.setValue(baseModel.getData());
                    }
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }

    /**
     * 获取录音url
     */
    public void getRecordUrl(String fileName) {
        Map<String, Object> map = new HashMap<>();
        map.put("fileName", fileName);
        addSubscribe(mRetrofitHelper.getRecordUrl(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        urlSuccess.setValue(baseModel.getData());
                    }
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }
}
